package com.etlmaster.transformer.funcs;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import com.etlmaster.executor.annotation.StepInstruction;
import com.etlmaster.executor.trans.core.Line;
import com.etlmaster.executor.trans.core.LineTransProcessor;
import com.etlmaster.executor.utils.LogWriter;



/**
 * 行级处理 日期格式转换
 * @author wugf
 * @date 2016-04-29
 */
@StepInstruction("timestampChange")
public class TransFieldTimestampFormat extends LineTransProcessor{

	private String sourceFormat;
	private String targetFormat;
	public TransFieldTimestampFormat(String sourceTab,String sourceCol,String targetTab,String targetCol,String changeCode,Map paramStr,Map var){
		
		super( sourceTab,sourceCol,targetTab,targetCol,changeCode, paramStr,var);
	
	}

	public String transTimestampFormat(String sourceValue,String sourceFormat, String targetFormat) {
		String targetValue=sourceValue;
		SimpleDateFormat sdf;
		try {
			sdf=new SimpleDateFormat(sourceFormat);
			Date date = sdf.parse(sourceValue);
			sdf = new SimpleDateFormat(targetFormat);
			targetValue = sdf.format(date);
		} catch (Exception e) {
			LogWriter.addLog("ERROR","时间格式转换异常,{}",e.getMessage());

		}
		return targetValue;
	}

	public boolean isSkip() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void transField(Line line) {
		String sourceValue = line.getField(getFieldIndex(line));
		line.replaceField(transTimestampFormat(sourceValue,sourceFormat,targetFormat), getFieldIndex(line));
		
	}


}
